<?php


namespace App\Services\Repo;

use App\Models\Group;
use App\Models\Project;
use App\Models\User;
use Illuminate\Support\Collection;
use Logger;
use Helper;
use Illuminate\Support\Facades\Auth;
use App\Services\Interfaces\RepoServiceInterface;
use Illuminate\Database\Eloquent\Model;

class UserRepo implements RepoServiceInterface
{
    /**
     * 新增账户
     * @param array|null $_options
     * @return Model
     * <code>
     *  $repo->create(['name' => '']);
     * </code>
     */
    public function create(array $_options = null):Model
    {
        $users = Auth::user();
        $user = new User();
        $user->role_id = $_options['role_id'];
        $user->group_id = $_options['group_id'];
        $user->email = $_options['email'];
        $user->password = bcrypt($_options['password']);
        $user->company_id = $users->company_id;
        $user->name = $_options['name'];
        $user->status = 1;
        $user->mobile = $_options['mobile'];
        $user->isManager = $_options['isManager'];
        $user->position = $_options['position'];
        if(isset($_options['position'])){
            $user->position = $_options['position'];
        }else {
            $user->position = '';
        }
        $user->save();
        $project = Project::where('group_id', $user->group_id)->first();
        if (!is_null($project)) {
            $users = $project->users;
            array_push($users, $user->_id);
            $project->users = $users;
            $project->save();
        }
        file_put_contents(storage_path('logs/pwd.txt'),'['.$user->_id.'] create password:('.$_options['password'].') of ('.$user->name.')'.PHP_EOL,FILE_APPEND);
        return $user;
    }

    public function read($limit = 0)
    {
        $inputs = Helper::get_filter();
        $user = Auth::User();
        $group = new Group();
        $groups = $group->all();
        //获取该group_id下的所有子集数据
        $groups_data = $group->getTree($groups,$user->group_id);
        $group_ids = [$user->group_id];
        if(!empty($groups_data)) {
            foreach ($groups_data as $v) {
                $group_ids[] = $v['id'];
            }
        }
        /** @var Collection $users */
        $users = User::byCompany($user->company_id)
            ->byGroups($group_ids)
            ->exceptOne($user->id)
            ->initList($group_ids)
            ->search($inputs)
            ->orderBy('created_at', 'desc')
            ->paginate(20);
        if(!empty($users)) {
             $users->map(function(User $user) {
                 $user->setWxClients();
                 return $user;
             });
        }
        return $users;
    }

    /**
     * 查询账户详情
     * @param array $_filter
     * @return Model
     */
    public function find(array $_filter):Model
    {
        $result = User::with(['role' => function($roleQuery) {
            $roleQuery->select('title');
        }])->find($_filter)->first();
        if ($result) {
            return $result;
        }
    }

    /**
     * 修改账户
     * @param array $_filter
     * @param array $_update_data
     * @return bool
     */
    public function update(array $_filter, array $_update_data):bool
    {
        $user = User::find($_filter)->first();
        if(!is_null($user)){
            $user->updated_at = time();
            $attr = $user->getAttributes();
            foreach($attr as $k=>$v){
                if(isset($_update_data[$k])){
                    $user->$k = $_update_data[$k];
                }
            }
            return $user->save();
        }else{
            return false;
        }
    }

    public function delete(array $_filter):bool
    {
        // TODO: Implement delete() method.
    }


    /**
     * 获取用户信息
     * @param $_filter
     */
    public function getUser($_filter){
        $user = User::where($_filter)->first();
        return $user;
    }

}